<?php
require_once('table.php');

class Projects extends Table{
	
	function Projects(){
		parent::Table();
		$this->load->model('admin/Projects_model');
		$this->model = $this->Projects_model;
	}
	
	function removeFeatured($id){
		$this->model->removeFeatured($id);
	}
	
	function check_client_datasource( $v ){
		if( empty($_POST['project_client_id']) || $_POST['project_client_id'] == '0' ){
			$_POST['project_client_datasource'] = null;
			return true;
		}else{
			if( !isset($_POST['project_client_datasource']) || empty($_POST['project_client_datasource'])){
				$this->validation->set_message('check_client_datasource', "You have not specified the data to use for the client listing.");
				return false;
			}else{
				return true;
			}
		}
	}
	
	function check_featured_projects( $v ){
		if( $v ){
			$v = $this->db->escape($v);
			$id = $this->db->escape($_POST['project_industry_id']);
			$pid = $this->db->escape($_POST['project_id']);
			$query = $this->db->query("SELECT project_id, project_title FROM projects WHERE project_industry_id = $id AND is_featured = true AND project_id <> $pid");
			if( $query && $query->num_rows() > 1){
				$links = array();
				foreach( $query->result_array() as $row){
					$links[] = '<a href="/admin/projects/removefeatured/' . $row['project_id'] . '" class="project_remove_featured" style="display: none;">' . $row['project_title'] . '</a>';
				}
				$this->validation->set_message('check_featured_projects', "Two projects are already featured in the industry you selected. Choose a different industry or click the following projects to remove their featured status: " . implode(', ', $links));
				return false;
			}
		}
		return true;
	}
}
?>